home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / slideshow / sources (complete) / misc.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  2.3 KB  |  85 lines

  1. import java.awt.Component;
  2. import java.awt.Image;
  3. import java.awt.MediaTracker;
  4. import java.net.URL;
  5. import java.net.MalformedURLException;
  6.  
  7. public class Misc
  8. {
  9.     /**
  10.      * Completely loads the Image referenced by the given filename.
  11.      * This will block until the image is loaded.
  12.      * @param filename the path of the Image to load.
  13.      * @param watcher the component to use to load the image.
  14.      * @param if true, the image location is treated as a resource relative to the
  15.      * watcher component's class file; if false the location is treated as an absolute file path.
  16.      * @return the loaded Image, or null if the loading fails.
  17.      */
  18.     public static Image loadImage(String filename, Component watcher, boolean isResource)
  19.     {
  20.         Image image = null;
  21.         
  22.         if (filename != null)
  23.         {
  24.             URL url = null;
  25.             
  26.             if (isResource)
  27.                 url = watcher.getClass().getResource(filename);
  28.             else
  29.             {
  30.                 try
  31.                 {
  32.                     url = new URL("file", "", filename);
  33.                 }
  34.                 catch(MalformedURLException exc)
  35.                 {
  36.                     exc.printStackTrace();
  37.                 }
  38.             }
  39.             
  40.             if (url == null)
  41.             {
  42.                 System.err.println("loadImage() could not find \"" + filename + "\"");
  43.             }
  44.             else
  45.             {
  46.                 image = watcher.getToolkit().getImage(url);
  47.                 if (image == null)
  48.                 {
  49.                     System.err.println("loadImage() getImage() failed for \"" + filename + "\"");
  50.                 }
  51.                 else
  52.                 {
  53.                     MediaTracker tracker = new MediaTracker(watcher);
  54.         
  55.                     try
  56.                     {
  57.                         tracker.addImage(image, 0);
  58.                         tracker.waitForID(0);
  59.                     }
  60.                     catch (InterruptedException e) { System.err.println("loadImage(): " + e); }
  61.                     finally
  62.                     {
  63.                         boolean isError = tracker.isErrorAny();
  64.                         if (isError)
  65.                         {
  66.                             System.err.println("loadImage() failed to load \"" + filename + "\"");
  67.                             int flags = tracker.statusAll(true);
  68.         
  69.                             boolean loading = 0 != (flags & MediaTracker.LOADING);
  70.                             boolean aborted = 0 != (flags & MediaTracker.ABORTED);
  71.                             boolean errored = 0 != (flags & MediaTracker.ERRORED);
  72.                             boolean complete = 0 != (flags & MediaTracker.COMPLETE);
  73.                             System.err.println("loading: " + loading);
  74.                             System.err.println("aborted: " + aborted);
  75.                             System.err.println("errored: " + errored);
  76.                             System.err.println("complete: " + complete);
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         
  83.         return image;
  84.     }
  85. }